package cnm.validator;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.util.XMLErrorHandler;
import org.xml.sax.SAXException;
public class XmlSchemaValidator {

    private static final Logger logger = Logger.getLogger(XmlSchemaValidator.class);

/*    
    public static void doValidate(){
        DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
        parserFactory.setNamespaceAware(true);
        DocumentBuilder parser;
        try {
            parser = parserFactory.newDocumentBuilder();
        
        Document document = parser.parse(new File("./metadata/pattern/address_object.xml"));

        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(new File("./metadata/pattern/address_object.xsd"));
        Schema schema = factory.newSchema(schemaFile);

        // create a Validator instance, which can be used to validate an instance document
        Validator validator = schema.newValidator();

        // validate the DOM tree

            validator.validate(new DOMSource(document));
        } catch (SAXException e) {
            System.out.println(e.getMessage());
            // instance document is invalid!
        } catch (ParserConfigurationException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
*/  
    public static void validate(InputStream in,String xsdfile) {
        SAXReader reader = new SAXReader();

        try {
            reader.setValidation(true);
            reader.setFeature("http://xml.org/sax/features/validation", true);
            reader.setFeature("http://apache.org/xml/features/validation/schema", true);
            reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
                    xsdfile);
    
            XMLErrorHandler errorHandler = new XMLErrorHandler();
            reader.setErrorHandler(errorHandler);
    
            reader.read(in);
            XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
            if (errorHandler.getErrors().hasContent()) {
                    writer.write(errorHandler.getErrors());
            } else {
                logger.debug("XSD validate success.");
            }
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }
    
    
}
